package day1201parking;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import day1130.JDBCUtil;

public class BillRecordDao {
	//向账单中添加收费记录
	public static void insert(BillRecordModel br) {
		Connection conn = null;
		PreparedStatement ps = null;
		String sql = "insert into record(id,salary) values(?,?)";
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, br.getId());
			ps.setBigDecimal(2, br.getSalary());
		    ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
	}
	
	//查找收费记录信息
	public static ArrayList<BillRecordModel> select(){
		ArrayList<BillRecordModel> list = new ArrayList<BillRecordModel>();
		String sql = "select * from record";
		Connection conn = null;
		PreparedStatement ps= null;
		ResultSet result = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			result = ps.executeQuery();
			while(result.next()) {
				BillRecordModel bill = new BillRecordModel();
				bill.setId(result.getString("id"));
				bill.setSalary(result.getBigDecimal("salary"));
				list.add(bill);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
			if(result!=null) {
				try {
					result.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				result = null;
			}
		}
		return list;
	}

}
